package com.finals.function.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.finals.function.R;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;

public class TouchCircle extends View implements AnimatorUpdateListener {

	public TouchCircle(Context context) {
		super(context);
		InitView(context);
	}

	public TouchCircle(Context context, AttributeSet attrs) {
		super(context, attrs);
		InitView(context);
	}

	ValueAnimator mAnimator;

	Paint mPaint;

	int backgroundColor = 0x44FFFFFF;

	int inBackgroundColor = 0xCCFFFFFF;

	int touchLength = 10;

	int touchPadding = 25;

	private void InitView(Context context) {
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		if (!isInEditMode()) {
			backgroundColor = context.getResources().getColor(R.color.trans_white2);
			inBackgroundColor = context.getResources().getColor(R.color.light_white);
		}
		mPaint.setColor(backgroundColor);
		mAnimator = ValueAnimator.ofFloat(1, 0);
		mAnimator.setInterpolator(new DecelerateInterpolator());
		mAnimator.setDuration(200);
		mAnimator.addUpdateListener(this);

		touchLength = context.getResources().getDimensionPixelSize(R.dimen.touch_length);
		touchPadding = context.getResources().getDimensionPixelSize(R.dimen.touch_padding);

		this.setPadding(touchPadding, touchPadding, touchPadding, touchPadding);

	}

	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
		if (mPaint != null) {
			// Finals 2016-7-5 绘制大圆环
			mPaint.setColor(backgroundColor);
			float radius = getRadius();
			canvas.drawCircle(radius, radius, radius - getPaddingTop(), mPaint);
			// Finals 2016-7-5 绘制小圆环
			mPaint.setColor(inBackgroundColor);
			float in_radius = getSmallCircle();
			canvas.drawCircle(radius + circleX, radius + circleY, in_radius - getPaddingTop(), mPaint);
		}
	}

	private float getRadius() {
		return getWidth() / 2;
	}

	private float getSmallCircle() {
		return getWidth() / 2 * 0.8F;
	}

	protected float XinView = 0, YinView = 0;
	protected float circleX = 0;
	protected float circleY = 0;
	double Xoffset = 0;
	double Yoffset = 0;

	boolean isStart = false;

	boolean isEnable = true;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			isEnable = true;
			isStart = false;
			isDone = false;
			XinView = event.getX();
			YinView = event.getY();
			break;
		case MotionEvent.ACTION_MOVE:
			if (!isEnable) {
				return true;
			}
			float tmpX = event.getX() - XinView;
			float tmpY = event.getY() - YinView;
			float MaxLenght = getRadius() - getSmallCircle() / 2;
			double radian = Math.atan(tmpX / tmpY);
			Xoffset = MaxLenght * Math.sin(radian);
			Xoffset = Math.abs(Xoffset);
			Yoffset = Math.sqrt(MaxLenght * MaxLenght - Xoffset * Xoffset);
			Yoffset = Math.abs(Yoffset);
			if (Math.abs(tmpX) > Xoffset) {
				if (tmpX < 0) {
					tmpX = (float) -Xoffset;
				} else {
					tmpX = (float) Xoffset;
				}
			}
			if (Math.abs(tmpY) > Yoffset) {
				if (tmpY < 0) {
					tmpY = (float) -Yoffset;
				} else {
					tmpY = (float) Yoffset;
				}
			}
			circleX = tmpX;
			circleY = tmpY;

			float tmp = MaxLenght - 5;
			if (circleY >= tmp) {
				if (!isStart) {
					isStart = true;
					Log.e("Finals", "向下滑动");
					mHandler.removeCallbacksAndMessages(null);
					mHandler.postDelayed(mRunnable1, 500);
				}
			} else
			// Finals
			if (circleY < -tmp) {
				if (!isStart) {
					isStart = true;
					Log.e("Finals", "向上滑动");
					mHandler.removeCallbacksAndMessages(null);
					mHandler.postDelayed(mRunnable2, 500);
				}
			} else
			// Finals
			if (circleX >= tmp) {
				if (!isStart) {
					isStart = true;
					Log.e("Finals", "向右滑动");
					mHandler.removeCallbacksAndMessages(null);
					mHandler.postDelayed(mRunnable3, 500);
				}
			} else {
				isStart = false;
			}

			postInvalidate();
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			if (!isEnable) {
				return true;
			}
			isStart = false;
			mHandler.removeCallbacksAndMessages(null);
			tmpX = event.getX() - XinView;
			tmpY = event.getY() - YinView;
			onScroll(tmpX, tmpY);
			mAnimator.cancel();
			mAnimator.start();
			break;
		default:
			break;
		}
		super.onTouchEvent(event);
		return true;
	}

	@Override
	public void onAnimationUpdate(ValueAnimator animator) {
		float tmp = (Float) animator.getAnimatedValue();
		circleX = tmp * circleX;
		circleY = tmp * circleY;
		postInvalidate();
	}

	public void onScroll(float distanceX, float distanceY) {
		if (Math.abs(distanceX) > Math.abs(distanceY)) {
			if (distanceX > touchLength) {
				OpenFunction(0);
			} else if (distanceX < -touchLength) {
				OpenFunction(3);
			}
		} else {
			if (distanceY > touchLength) {
				OpenFunction(1);
			} else if (distanceY < -touchLength) {
				OpenFunction(2);
			}
		}
	}

	public void OpenFunction(int i) {
		this.cancelLongPress();
	}

	protected void ResetLocation() {
		XinView = 0;
		YinView = 0;
		circleX = 0;
		circleY = 0;
		Xoffset = 0;
		Yoffset = 0;
	}

	boolean isDone = false;

	Runnable mRunnable1 = new Runnable() {

		@Override
		public void run() {
			Log.e("Finals", "调用完毕");
			OpenFunction(4);
			isDone = true;

			isEnable = false;
			mAnimator.cancel();
			mAnimator.start();
		}
	};

	Runnable mRunnable2 = new Runnable() {

		@Override
		public void run() {
			Log.e("Finals", "调用完毕");
			OpenFunction(5);
			isDone = true;

			isEnable = false;
			mAnimator.cancel();
			mAnimator.start();
		}
	};

	Runnable mRunnable3 = new Runnable() {

		@Override
		public void run() {
			Log.e("Finals", "调用完毕");
			OpenFunction(6);
			isDone = true;

			isEnable = false;
			mAnimator.cancel();
			mAnimator.start();
		}
	};

	Handler mHandler = new Handler(Looper.getMainLooper());
}